package cn.wp.system.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.system.domain.Dictionary;
import cn.wp.system.query.DictionaryQuery;
import cn.wp.system.service.IDictionaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典接口
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    /**
     * 根据id查询一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据字典对象",notes = "根据id查询")
    @GetMapping("/{id}")
    public Dictionary findOne (@PathVariable("id") Long id){
        return dictionaryService.findOne(id);
    }

    /**
     * 查询所有对象
     * @return
     */
    @ApiOperation(value = "查询所有数据字典对象")
    @GetMapping
    public List<Dictionary> findAll (){
        return dictionaryService.findAll();
    }

    /**
     * 新增和修改
     * @param dictionary
     * @return
     */
    @ApiOperation(value = "数据字典添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionary dictionary){
        try {
            if (dictionary.getId() == null){
                dictionaryService.add(dictionary);
            } else {
                dictionaryService.update(dictionary);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @param dictionaryQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Dictionary> queryPage(@RequestBody DictionaryQuery dictionaryQuery){
        return dictionaryService.queryPage(dictionaryQuery);
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据字典对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            dictionaryService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("字典中存在数据，请先删除数据！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     *//*
    @ApiOperation(value = "批量删除",notes = "")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            dictionaryService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }*/
}
